﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.Web.UI.Controls;
using ZX.BLL.Extend;
using ZX.Common;
using ZX.EHR.Library;
using ZX.BLL;

namespace ZX.WebApp.EHR
{
    public partial class hrSalaryAjax :BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string flag = this.GetStringFromQueryString("flag","");
            string json = string.Empty;
            try
            {
                switch (flag)
                {
                    case "getfieldbytable": //获取表的字段名
                        string table = this.GetStringFromQueryString("table", "");
                        string sqlsys = "select a.name from sys.columns a,sys.types b where [object_id]=object_id('" + table + "') and a.system_type_id=b.system_type_id and b.name in('numeric','int','varchar','char','float','decimal')";
                        BaseBussnies bll = new BaseBussnies();
                        System.Data.DataTable dt = bll.GetDataBySqlComm(sqlsys);
                        json = Globals.DataTableToCJson(dt);
                        break;
                    case "saveSalaryDeptSolution": //保存薪资方案适用部门或岗位或个人
                        json = SaveSalaryDeptSolution();
                        break;
                    case "calcEmpSalary": //计算员工个人薪资
                        string empId = this.GetStringFromQueryString("empId", "");
                        if (empId != string.Empty)
                        {
                            try
                            {
                                salSalarylErrorLog salaryErrLog = new salSalarylErrorLog();
                                BaseBussnies errBll = new BaseBussnies(salaryErrLog);
                                salaryErrLog.moduleName = this.GetType().Module.Name;
                                salaryErrLog.className = this.GetType().Name;
                                salaryErrLog.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
                                salaryErrLog.errMsg = "start calc employee salary!";
                                salaryErrLog.errDate = DateTime.Now;
                                errBll.Insert();
                            }
                            catch { }
                            string yearMonth = this.GetStringFromQueryString("yearMonth", "");
                            SalaryFormulaResolve.CalSingleEmployeeSalarySQL(empId, yearMonth);
                        }
                        json = "0";
                        break;
                    case "calcDeptSalary": //计算部门员工薪资
                        string deptId = this.GetStringFromQueryString("deptId", "");
                        if (deptId != string.Empty)
                        {
                            string deptYearMonth = this.GetStringFromQueryString("yearMonth", "");
                            SalaryFormulaResolve.CalDeptEmployeeSalarySQL(deptId, deptYearMonth);
                        }
                        json = "0";
                        break;
                    case "deleteSalaryItemOfSolution":  //删除薪资方案中的薪资项
                        string salaryItemId = this.GetStringFromQueryString("id", "");
                        salSalaryItem salaryItem = new salSalaryItem();
                        BaseBussnies salaryItemBll = new BaseBussnies(salaryItem);
                        salaryItem = salaryItemBll.GetEntityById(salaryItemId) as salSalaryItem;
                        salaryItemBll.delete(salaryItem);
                        json = "0";
                        break;
                    case "savebasesalary": //保存新增的基本薪资
                        string empId2 = this.GetStringFromQueryString("empId", "");
                        string salary = this.GetStringFromQueryString("salary", "");//前台工资
                        string salaryHou = this.GetStringFromQueryString("salaryHou", "");//后台工资
                        string PositiveBaseSalary = this.GetStringFromQueryString("PositiveBaseSalary", "");//转正工资
                        string edate = this.GetStringFromQueryString("edate", "");//生效日期
                        string exdate = this.GetStringFromQueryString("exdate", "");//失效日期
                        string coff = this.GetStringFromQueryString("coff", "1");//佣金系数
                        if (empId2 != "" && salary != "" && salaryHou != "" && edate != "")
                        {
                            bool f = EncryptData.EncryptSalary.SaveBaseData(empId2, salary, salaryHou, edate, exdate, this.User.UserID.ToString(), coff, PositiveBaseSalary);
                            if (f) json = "0";
                        }
                        break;
                    case "checkpwd": //检查用户密码
                        string userId = this.User.UserID.ToString();
                        string userPwd = this.GetStringFromQueryString("userPwd", string.Empty);
                        bool checkFlag = EncryptData.EncryptSalary.CheckUserPwd(userId, userPwd);
                        if (checkFlag) json = "0";
                        break;
                    case "saveinterceptionsalary": //保存截留薪资
                        string id = this.GetStringFromQueryString("id", "");
                        string empId3 = this.GetStringFromQueryString("empId", "");
                        string salary2 = this.GetStringFromQueryString("salary", "");
                        int year = this.GetIntFromQueryString("year", -1);
                        int month = this.GetIntFromQueryString("month", -1);
                        string des = this.GetStringFromQueryString("des", "");
                        if (empId3 != "" && salary2 != "" && year != -1 && month != -1)
                        {
                            bool f = EncryptData.EncryptSalary.SaveInterceptionSalary(id, empId3, year, month, salary2, des, this.User.UserID.ToString());
                            if (f) json = "0";
                        }
                        break;
                    case "savereissuesalary": //保存补发工资
                        string id2 = this.GetStringFromQueryString("id", "");
                        string empId4 = this.GetStringFromQueryString("empId", "");
                        string salary3 = this.GetStringFromQueryString("salary", "");
                        int year2 = this.GetIntFromQueryString("year", -1);
                        int month2 = this.GetIntFromQueryString("month", -1);
                        int reissueYear = this.GetIntFromQueryString("reissueYear", -1);
                        int reissueMonth = this.GetIntFromQueryString("reissueMonth", -1);
                        string des2 = this.GetStringFromQueryString("des", "");
                        if (empId4 != "" && salary3 != "" && year2 != -1 && month2 != -1 && reissueYear != -1 && reissueMonth != -1)
                        {
                            bool f = EncryptData.EncryptSalary.SaveReissueSalary(id2, empId4, year2, month2, reissueYear, reissueMonth, salary3, des2, this.User.UserID.ToString());
                            if (f) json = "0";
                        }
                        break;
                    case "saveovertimefee"://保存加班工资
                        string overtime_empId = this.GetStringFromQueryString("empId", "");
                        string workDayOvertimeFee = this.GetStringFromQueryString("workfee", "0");
                        string weekDayOvertimeFee = this.GetStringFromQueryString("weekfee", "0");//生效日期
                        string holidayOvertimeFee = this.GetStringFromQueryString("holidayfee", "0");//失效日期
                        string dutyFee = this.GetStringFromQueryString("dutyFee", "0");//失效日期
                        string meetingsOvertimeFee = this.GetStringFromQueryString("meetingfee", "0");//失效日期
                        string overtime_year = this.GetStringFromQueryString("year", "");//年份
                        string overtime_month = this.GetStringFromQueryString("month", "");//月份
                        string overtime_des = this.GetStringFromQueryString("des", "");//描述
                        if (overtime_empId != "" && overtime_year != "" && overtime_month != "")
                        {
                            bool f = EncryptData.EncryptSalary.SaveOvertimeFee(overtime_empId,
                                                                               workDayOvertimeFee,
                                                                               weekDayOvertimeFee,
                                                                               holidayOvertimeFee,
                                                                               dutyFee,
                                                                               meetingsOvertimeFee,
                                                                               overtime_year,
                                                                               overtime_month,
                                                                               overtime_des,
                                                                               this.User.UserID.ToString());
                            if (f) json = "0";
                        }
                        break;
                    case "savebasicsubsidy": //保存补贴基数
                        string basicSubsidy_empId = this.GetStringFromQueryString("empId", "");
                        string houseSubsidy = this.GetStringFromQueryString("houseSubsidy", "0"); //住房补贴
                        string communicationSubsidy = this.GetStringFromQueryString("communicationSubsidy", "0");//通迅补贴
                        string diningSubsidy = this.GetStringFromQueryString("diningSubsidy", "0");//餐饮补贴
                        string travelSubsidy = this.GetStringFromQueryString("travelSubsidy", "0");//差旅补贴
                        string otherSubsidy = this.GetStringFromQueryString("otherSubsidy", "0");//其他补贴
                        string fixedSubsidy = this.GetStringFromQueryString("fixedSubsidy", "0");//定额补贴
                        string startDate = this.GetStringFromQueryString("startDate", "");//开始日期
                        string endDate = this.GetStringFromQueryString("endDate", "");//结束日期
                        string basicSubsidy_des = this.GetStringFromQueryString("des", "");//描述
                        if (basicSubsidy_empId != "" && startDate != "" && endDate != "")
                        {
                            bool f = EncryptData.EncryptSalary.SaveBasicSubsidy(basicSubsidy_empId,
                                                                                houseSubsidy,
                                                                                communicationSubsidy,
                                                                                diningSubsidy,
                                                                                travelSubsidy,
                                                                                otherSubsidy,
                                                                                fixedSubsidy,
                                                                                startDate, endDate, basicSubsidy_des,
                                                                                this.User.UserID.ToString());
                            if (f) json = "0";
                        }
                        break;
                    case "savelargeclassbonus": //保存大课奖金
                        string largeClassBonus_empId = this.GetStringFromQueryString("empId", "");
                        string largeClassBonus = this.GetStringFromQueryString("largeClassBonus", "0");//大课奖金
                        string largeClassBonus_year = this.GetStringFromQueryString("year", "");//年份
                        string largeClassBonus_month = this.GetStringFromQueryString("month", "");//月份
                        string largeClassBonus_des = this.GetStringFromQueryString("des", "");//描述
                        if (largeClassBonus_empId != "" && largeClassBonus_year != "" && largeClassBonus_month != "")
                        {
                            bool f = EncryptData.EncryptSalary.SaveLargeClassBonus(largeClassBonus_empId,
                                                                               largeClassBonus,
                                                                               largeClassBonus_year,
                                                                               largeClassBonus_month, largeClassBonus_des,
                                                                               this.User.UserID.ToString());
                            if (f) json = "0";
                        }
                        break;
                    case "saveperformancebonus": //保存绩效奖金
                        string performancebonus_empId = this.GetStringFromQueryString("empId", "");
                        string projectBonus = this.GetStringFromQueryString("projectBonus", "0"); //项目奖金
                        string quarterBonus = this.GetStringFromQueryString("quarterBonus", "0");//季度奖金
                        string yearendBonus = this.GetStringFromQueryString("yearendBonus", "0");//年终奖
                        string otherBonus = this.GetStringFromQueryString("otherBonus", "0");//其他奖金
                        string performancebonus_year = this.GetStringFromQueryString("year", "");//年份
                        string performancebonus_month = this.GetStringFromQueryString("month", "");//月份
                        string performancebonus_des = this.GetStringFromQueryString("des", "");//描述
                        if (performancebonus_empId != "" && performancebonus_year != "" && performancebonus_month != "")
                        {
                            bool f = EncryptData.EncryptSalary.SavePerformanceBonus(performancebonus_empId,
                                                                                    projectBonus,
                                                                                    quarterBonus,
                                                                                    yearendBonus,
                                                                                    otherBonus,
                                                                                    performancebonus_year,
                                                                                    performancebonus_month,
                                                                                    performancebonus_des,
                                                                                    this.User.UserID.ToString());
                            if (f)
                            {
                                json = "0";
                            }
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                try
                {
                    salSalarylErrorLog salaryErrLog = new salSalarylErrorLog();
                    BaseBussnies errBll = new BaseBussnies(salaryErrLog);
                    salaryErrLog.moduleName = this.GetType().Module.Name;
                    salaryErrLog.className = this.GetType().Name;
                    salaryErrLog.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
                    salaryErrLog.errMsg = ex.Message;
                    salaryErrLog.errDate = DateTime.Now;
                    errBll.Insert();
                }
                catch { }
            }
            Response.ContentType = "text/plain";
            Response.Write(json);
            Response.End();
        }

        /// <summary>
        /// 保存薪资方案适用部门，岗位或个人
        /// </summary>
        private string SaveSalaryDeptSolution()
        {
            try
            {
                string type = this.GetStringFromQueryString("type", "");//ID类型
                int flag = 0;
                try
                {
                    flag = int.Parse(type);
                }
                catch(Exception e) {
                    return "e" + e.Message;
                }
                string ids = this.GetStringFromQueryString("ids","");
                string SolutionID = this.GetStringFromQueryString("SolutionID","");
                salDeptSolution instance = new salDeptSolution();
                ZX.BLL.Extend.BaseBussnies bll = new ZX.BLL.Extend.BaseBussnies(instance);
                //先删除
                bll.delete("SolutionID='" + SolutionID + "' and Flag=" + flag);
                //配置方案
                if (ids != string.Empty && SolutionID != string.Empty)
                {
                    foreach (string deptId in ids.Split(','))
                    {
                        if (deptId.Trim() == "") continue;
                        salDeptSolution tempDeptCalendar = new salDeptSolution();
                        tempDeptCalendar = new salDeptSolution();
                        tempDeptCalendar.ID = Guid.NewGuid();
                        tempDeptCalendar.SolutionID = new Guid(SolutionID);
                        tempDeptCalendar.DeptID = new Guid(deptId);
                        tempDeptCalendar.Flag = flag;
                        tempDeptCalendar.AddUser = this.User.UserID;
                        tempDeptCalendar.AddTime = DateTime.Now;
                        bll.Insert(tempDeptCalendar);
                    }
                }
                return "1";
            }
            catch (Exception e)
            {
                try
                {
                    salSalarylErrorLog salaryErrLog = new salSalarylErrorLog();
                    BaseBussnies errBll = new BaseBussnies(salaryErrLog);
                    salaryErrLog.moduleName = this.GetType().Module.Name;
                    salaryErrLog.className = this.GetType().Name;
                    salaryErrLog.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
                    salaryErrLog.errMsg = e.Message;
                    salaryErrLog.errDate = DateTime.Now;
                    errBll.Insert();
                }
                catch { }
                return "e"+e.Message;
            }
        }
    }
}